package vn.sunnet.backviet01.game.coloa.util;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

public class MyTiledButton extends Sprite{

	final TextureRegion[] mTextureRegion;

	private final int mStateCount;
	private OnTiledButtonClickListener mOnClickListener;

	private boolean mEnabled = true;
	private State mState;
	
	private final int id;

	// ===========================================================
	// Constructors
	// ===========================================================
	
	public MyTiledButton(int id, float pX, float pY, final OnTiledButtonClickListener pOnClickListener, TextureRegion... pTextureRegion) {
		super(pX, pY, pTextureRegion[0]);
		mTextureRegion = pTextureRegion;
		this.mOnClickListener = pOnClickListener;
		this.mStateCount = pTextureRegion.length;
		this.changeState(State.NORMAL);
		this.id = id;
	}
	
	public MyTiledButton(int id, final OnTiledButtonClickListener pOnClickListener, TextureRegion... pTextureRegion) {
		super(0, 0, pTextureRegion[0]);

		mTextureRegion = pTextureRegion;
		this.mOnClickListener = pOnClickListener;
		this.mStateCount = pTextureRegion.length;
		this.changeState(State.NORMAL);
		this.id = id;
	}

		// ===========================================================
		// Getter & Setter
		// ===========================================================

		public int getID() {
			return id;
		}
		
		public boolean isEnabled() {
			return this.mEnabled;
		}

		public void setEnabled(final boolean pEnabled) {
			this.mEnabled = pEnabled;

			if(this.mEnabled && this.mState == State.DISABLED) {
				this.changeState(State.NORMAL);
			} else if(!this.mEnabled) {
				this.changeState(State.DISABLED);
			}
		}

		public boolean isPressed() {
			return this.mState == State.PRESSED;
		}

		public State getState() {
			return this.mState;
		}

		public void setOnClickListener(final OnTiledButtonClickListener pOnClickListener) {
			this.mOnClickListener = pOnClickListener;
		}

		// ===========================================================
		// Methods for/from SuperClass/Interfaces
		// ===========================================================

		@Override
		public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
			if(!this.isEnabled()) {
				this.changeState(State.DISABLED);
			} else if(pSceneTouchEvent.isActionDown()) {
				this.changeState(State.PRESSED);
			} else if(pSceneTouchEvent.isActionCancel() || !this.contains(pSceneTouchEvent.getX(), pSceneTouchEvent.getY())) {
				this.changeState(State.NORMAL);
			} else if(pSceneTouchEvent.isActionUp() && this.mState == State.PRESSED) {
				this.changeState(State.NORMAL);

				if(this.mOnClickListener != null) {
					this.mOnClickListener.onClick(this, pTouchAreaLocalX, pTouchAreaLocalY);
				}
			}

			return true;
		}

		@Override
		public boolean contains(final float pX, final float pY) {
			if(!this.isVisible()) {
				return false;
			} else {
				return super.contains(pX, pY);
			}
		}

		// ===========================================================
		// Methods
		// ===========================================================

		private void changeState(final State pState) {
			if(pState == this.mState) {
				return;
			}

			this.mState = pState;

			final int stateTiledTextureRegionIndex = this.mState.getTiledTextureRegionIndex();
			if(stateTiledTextureRegionIndex >= this.mStateCount) {
				this.updateTextureRegion(mTextureRegion[0], true);
				
			} else {
				this.updateTextureRegion(mTextureRegion[stateTiledTextureRegionIndex], true);
			}
		}

		// ===========================================================
		// Inner and Anonymous Classes
		// ===========================================================

		public interface OnTiledButtonClickListener {
			// ===========================================================
			// Constants
			// ===========================================================

			// ===========================================================
			// Methods
			// ===========================================================

			public void onClick(final MyTiledButton pButtonSprite, final float pTouchAreaLocalX, final float pTouchAreaLocalY);

		}

		public static enum State {
			// ===========================================================
			// Elements
			// ===========================================================

			NORMAL(0),
			PRESSED(1),
			DISABLED(2);

			// ===========================================================
			// Constants
			// ===========================================================

			// ===========================================================
			// Fields
			// ===========================================================

			private final int mTiledTextureRegionIndex;

			// ===========================================================
			// Constructors
			// ===========================================================

			private State(final int pTiledTextureRegionIndex) {
				this.mTiledTextureRegionIndex = pTiledTextureRegionIndex;
			}

			// ===========================================================
			// Getter & Setter
			// ===========================================================

			public int getTiledTextureRegionIndex() {
				return this.mTiledTextureRegionIndex;
			}

			// ===========================================================
			// Methods for/from SuperClass/Interfaces
			// ===========================================================

			// ===========================================================
			// Methods
			// ===========================================================

			// ===========================================================
			// Inner and Anonymous Classes
			// ===========================================================
		}
	
	
}
